﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace IssuesClient
{
    /// <summary>
    /// Interaction logic for UserEdit.xaml
    /// </summary>
    public partial class UserEdit : Window
    {
        public UserEdit()
        {
            InitializeComponent();
            this.PriviledgeComboBox.ItemsSource = Enum.GetValues(typeof(IssueServiceReference.PrivilidgeCondition));
            this.DataContext = new IssueServiceReference.User();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.Owner != null && this.Owner is Window1)
            {
                IssueServiceReference.IssueServiceClient ownerClient = this.getOwnerServiceClient();
                if (ownerClient != null)
                {
                    if (ownerClient.LoginIdExists(this.LoginIDTextBox.Text))
                    {
                        MessageBox.Show("Login ID is already taken");
                        return;
                    }
                    else
                    {
                        (this.DataContext as IssueServiceReference.User).password = this.EmployeePassword.Password;
                        this.DialogResult = true;
                    }
                }
            }
        }
        private void validate()
        {
            bool text_Valid = (this.EmployeeNameTextBox.Text != string.Empty && this.LoginIDTextBox.Text != string.Empty);
            bool password_valid = this.EmployeePassword.Password != string.Empty;
            bool email_valid = (Regex.Match(this.EmployeeEmailTextBox.Text, @"^[a-zA-Z0-9_-]+@[a-zA-Z]+[.]{1}[a-zA-Z]+$").Success);
            bool priviledge_selected = (this.PriviledgeComboBox.SelectedItem != null);

            this.OKButton.IsEnabled = (text_Valid && password_valid && priviledge_selected && email_valid);
        }

        private void Validate_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.validate();
        }

        private void PriviledgeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.validate();
        }

        private void EmployeePassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            this.validate();
        }

        private IssueServiceReference.IssueServiceClient getOwnerServiceClient()
        {
            IssueServiceReference.IssueServiceClient myClient = null;

            if (this.Owner != null && this.Owner is Window1)
            {
                Window1 myOwner = this.Owner as Window1;
                if (myOwner.ServiceClient != null)
                {
                    myClient = myOwner.ServiceClient;
                }
            }
            return myClient;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
